package forum.command.console;

import forum.command.Command;
import forum.command.CommandContext;
import forum.entities.Category;
import forum.entities.Topic;
import forum.services.UserService;

import java.math.BigInteger;
import java.util.List;

public class OpenCategoryCommand implements Command {

    private BigInteger categoryId;
    private Category currentCategory;

    private UserService userService = new UserService();

    @Override
    public void execute() {
        currentCategory = userService.getCategory(categoryId);
        if (currentCategory == null) {
            throw new IllegalArgumentException("Category with id " + categoryId.toString() + " doesn't exist");
        }
        List<Category> subCategories = userService.getChildCategories(currentCategory);
        List<Topic> topics = userService.getTopics(currentCategory);
        System.out.println("Categories :");
        for (Category category : subCategories) {
            System.out.println("id (" + category.getId() + ") Category\n" +
                    category.getCategoryName()+System.lineSeparator() + "created by " +
                    userService.getAuthor(category.getAuthorId()).getNickName());
            System.out.println();
        }
        System.out.println("Topics :");
        for (Topic topic : topics) {
            System.out.println("id (" + topic.getId() + ") Topic\n" +
                    topic.getTopicName()
                    +System.lineSeparator() + "created by " +
                    userService.getAuthor(topic.getAuthorId()).getNickName());
            System.out.println();
        }

    }

    @Override
    public void setParameters(String... args) {
        categoryId = new BigInteger(args[0]);
    }

    @Override
    public void setContext(CommandContext context) {
        currentCategory = context.getCategory();
    }

    @Override
    public void updateContext(CommandContext context) {
        context.setCategory(currentCategory);
    }

    @Override
    public String help() {
        return "Type 'open_cat | %categoryId%' ";
    }
}
